package com.yupi.oj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.oj.common.BaseResponse;
import com.yupi.oj.common.ErrorCode;
import com.yupi.oj.common.ResultUtils;
import com.yupi.oj.exception.BusinessException;
import com.yupi.oj.exception.ThrowUtils;
import com.yupi.oj.model.dto.questionSubmit.QuestionSubmitAddRequest;
import com.yupi.oj.model.dto.questionSubmit.QuestionSubmitQueryRequest;
import com.yupi.oj.model.entity.QuestionSubmit;
import com.yupi.oj.model.entity.User;
import com.yupi.oj.model.vo.CustomPage;
import com.yupi.oj.model.vo.QuestionSubmitSimpleVO;
import com.yupi.oj.model.vo.QuestionSubmitVO;
import com.yupi.oj.model.vo.UserAnswerStatisticsVO;
import com.yupi.oj.service.QuestionSubmitService;
import com.yupi.oj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子题目提交接口
 */
@RestController
@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    /**
     * 题目提交
     *
     * @param questionSubmitAddRequest
     * @param request
     * @return
     */
    @PostMapping("/")
    public BaseResponse<QuestionSubmitVO> doQuestion(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
                                                     HttpServletRequest request) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能题目提交
        User loginUser = userService.getLoginUser(request);
        QuestionSubmitVO result = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 分页获取列表
     *
     * @param questionSubmitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitVOByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
                                                                           HttpServletRequest request) {
        long current = questionSubmitQueryRequest.getCurrent();
        long size = questionSubmitQueryRequest.getPageSize();
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, request));
    }

    /**
     * 用户解题统计
     */
    @GetMapping("/analysis")
    public BaseResponse<UserAnswerStatisticsVO> getUserAnswerStatisticVO(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        UserAnswerStatisticsVO userAnswerStatisticsVO = questionSubmitService.getUserAnswerStatisticVO(loginUser);
        return ResultUtils.success(userAnswerStatisticsVO);
    }


    /**
     * 用户题目提交基本信息
     */
    @PostMapping("/list/simple/page/vo")
    public BaseResponse<CustomPage<QuestionSubmitSimpleVO>> listQuestionSubmitSimpleVOByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
                                                                                             HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if(questionSubmitQueryRequest.getUserId() == null){
            questionSubmitQueryRequest.setUserId(loginUser.getId());
        }
        CustomPage<QuestionSubmitSimpleVO> questionSubmitSimpleVOList = questionSubmitService.listQuestionSubmitSimpleVO(questionSubmitQueryRequest, loginUser);
        return ResultUtils.success(questionSubmitSimpleVOList);
    }
    /**
     * 根据题目Id获取用户提交列表
     */
    @PostMapping("/list/my/by/questionId")
    public BaseResponse<CustomPage<QuestionSubmitSimpleVO>> listQuestionSubmitSimpleVOByQuestionId(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        CustomPage<QuestionSubmitSimpleVO> questionSubmitSimpleVOList = questionSubmitService.listQuestionSubmitSimpleVOByQuestionId(questionSubmitQueryRequest, loginUser);
        return ResultUtils.success(questionSubmitSimpleVOList);
    }
    /**
     * 根据Id获取题目提交信息
     */
    @GetMapping("/get/by/id")
    public BaseResponse<QuestionSubmitVO> getQuestionSubmitById(Long id,HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        QuestionSubmit questionSubmit = questionSubmitService.getById(id);
        ThrowUtils.throwIf(questionSubmit == null, ErrorCode.NOT_FOUND_ERROR,"该提交记录不存在");
        // 只有本人与管理员可获取
        if(!userService.isAdmin(loginUser) || !questionSubmit.getUserId().equals(loginUser.getId())){
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR);
        }
        return ResultUtils.success(QuestionSubmitVO.objToVo(questionSubmit));
    }
}
